package com.li.controller;

import com.common.util.MyJSONUtil;
import com.li.model.MyMassage;
import com.li.model.User;
import com.li.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.file.OpenOption;
import java.util.List;
import java.util.Optional;

@Api(tags = "用户管理")
@RestController
public class UserManageController {
    @Autowired
    UserService userService;
    //用户增删改
    @ApiOperation("添加用户")
    @PostMapping("/admin/user/add")
    public ResponseEntity<MyMassage> useradd(User user){
        Optional<User> userOptional = userService.queryUserByName(user.getUsername());
        if (userOptional.isPresent()){
            return  ResponseEntity.ok(new MyMassage("此用户已存在！",null,400));
        }
        userService.addUser(user);
        System.out.println("成功添加用户"+user);
        return ResponseEntity.ok(new MyMassage("添加成功",user,200));
    }
    @ApiOperation("删除用户")
    @GetMapping("/admin/user/delete/{id}")
    public ResponseEntity<MyMassage> userDelete(@PathVariable long id){
        Optional<User> userOptional = userService.queryUserById(id);
        if (!userOptional.isPresent()){
            return ResponseEntity.ok(new MyMassage("此用户不存在无法删除",id,400));
        }
        userService.deleteUser(id);
        return ResponseEntity.ok(new MyMassage("成功删除！",id,200));
    }
    @ApiOperation("更新用户")
    @PostMapping("/admin/user/update")
    public ResponseEntity<MyMassage> userUpdate(User user){
        Optional<User> userOptional = userService.queryUserById(user.getId());
        if (!userOptional.isPresent()){
            ResponseEntity.notFound().build();
        }
        userService.updateUser(user) ;
        return ResponseEntity.ok(new MyMassage("成功更新此用户",user.getUsername(),200));
    }
    @ApiOperation("通过用户id查询用户")
    @GetMapping("/admin/user/query/{id}")
    public ResponseEntity<MyMassage> queryById(@PathVariable long id){
        Optional<User> userOptional = userService.queryUserById(id);
        return userOptional.map(user -> ResponseEntity.ok(new MyMassage("查询用户", user, 200))).orElseGet(() -> ResponseEntity.ok(new MyMassage("查无此人",null,200)));
    }
    //gai
    @ApiOperation("查询所有用户")
    @GetMapping("/admin/user/queryAll")
    public ResponseEntity<MyMassage> queryAll(){
        Optional<List<User>> users = userService.queryAllUser();
        return users.map(userList -> ResponseEntity.ok(new MyMassage("用户列表", userList, 200))).orElseGet(() -> ResponseEntity.notFound().build());
    }

    @ApiOperation("通过用户id查询用户（公用）")
    @GetMapping("/com/user/queryById/{id}")
    public ResponseEntity<MyMassage> queryById1(@PathVariable long id){
        Optional<User> userOptional = userService.queryUserById(id);
        return userOptional.map(user -> ResponseEntity.ok(new MyMassage("查询用户", user.getUsername(), 200))).orElseGet(() -> ResponseEntity.ok(new MyMassage("查无此人",null,200)));
    }
    @ApiOperation("通过用户名字查询用户（公用）")
    @GetMapping("/com/user/queryByName")
    public ResponseEntity<MyMassage> queryByUsername(String username){
        Optional<User> userOptional = userService.queryUserByName(username);
        return userOptional.map(user -> ResponseEntity.ok(new MyMassage("查询用户", user.getUsername(), 200))).orElseGet(() -> ResponseEntity.ok(new MyMassage("查无此人",null,200)));
    }
//    @ApiOperation("通过用户名字查询用户职位（公用）")
//    @GetMapping("/com/user/query")
//    public String queryUserPosition(String username){
//        return MyJSONUtil.toJsonString(userService.getUserPosition(userService.queryUserByName(username).getGrade()));
//    }
    @ApiOperation("更新用户(公用)")
    @PostMapping("/com/user/update")
    public ResponseEntity<MyMassage> userUpdate1(User user){
        Optional<User> userOptional = userService.queryUserById(user.getId());
        if (!userOptional.isPresent()){
            ResponseEntity.notFound().build();
        }
        userService.updateUser(user) ;
        return ResponseEntity.ok(new MyMassage("成功更新此用户",user.getUsername(),200));
    }
    @ApiOperation("添加用户(普通用户注册)")
    @PostMapping("/com/user/add")
    public ResponseEntity<MyMassage> useradd1(@RequestBody User user){
        Optional<User> userOptional = userService.queryUserByName(user.getUsername());
        if (userOptional.isPresent()){
            return  ResponseEntity.ok(new MyMassage("此用户已存在！",null,400));
        }
        userService.addUser(user);
        System.out.println("成功添加用户"+user);
        return ResponseEntity.ok(new MyMassage("添加成功",user.getUsername(),200));
    }
    @ApiOperation("得到当前用户信息")
    @PostMapping("/com/user/getCurrentUser")
    public ResponseEntity<MyMassage> getCurrentUser(){
        Subject currentSubject = SecurityUtils.getSubject();
        Session session = currentSubject.getSession();
        User user = (User) session.getAttribute("USER_SESSION");
        if (user==null){
            return ResponseEntity.ok(new MyMassage("请先登录", null, 400));
        }


        return ResponseEntity.ok(new MyMassage("得到当前用户",user,200));
    }
}
